package com.simple.demo.clone;

import java.util.Objects;

/**
 * 对象浅克隆
 */
public class User implements Cloneable {

    private String id;



    private String name;

    private String address;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    /**
     * 对象的浅克隆，克隆对象和被克隆对象在内存中都指向同一块内存地址，
     * 任何对象的改变都会引起全部改变
     * @return
     */
    @Override
    protected Object clone()  {
        User user = null;
        try{
            user = (User) super.clone();
        }catch (CloneNotSupportedException ce){
            ce.printStackTrace();

        }
        return  user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(id, user.id) &&
                Objects.equals(name, user.name) &&
                Objects.equals(address, user.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, address);
    }
}
